﻿Public Class viewprofile1
    Inherits System.Web.UI.Page

	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		If Not Page.IsPostBack Then
			LoadProvince1()
			LoadProvince2()

			LoadDistinct1()
			LoadDistinct2()

			LoadSubDistinct1()
			LoadSubDistinct2()

			LoadPostCode1()
			LoadPostCode2()

			If GlobalApp.ProfileInfo IsNot Nothing Then
				LoadData()
			End If
		End If
	End Sub

	Private Sub LoadProvince1()
		OFFICER_CARD_ADDRESS_PROVINCE.DataTextField = "PROVINCE_NAME"
		OFFICER_CARD_ADDRESS_PROVINCE.DataValueField = "PROVINCE_ID"
		OFFICER_CARD_ADDRESS_PROVINCE.DataSource = BL.Province.GetAll
		OFFICER_CARD_ADDRESS_PROVINCE.DataBind()
		OFFICER_CARD_ADDRESS_PROVINCE.Items.Insert(0, New ListItem("--กรุณาระบุจังหวัด--", "0"))
	End Sub

	Private Sub LoadProvince2()
		OFFICER_MAIL_ADDRESS_PROVINCE.DataTextField = "PROVINCE_NAME"
		OFFICER_MAIL_ADDRESS_PROVINCE.DataValueField = "PROVINCE_ID"
		OFFICER_MAIL_ADDRESS_PROVINCE.DataSource = BL.Province.GetAll
		OFFICER_MAIL_ADDRESS_PROVINCE.DataBind()
		OFFICER_MAIL_ADDRESS_PROVINCE.Items.Insert(0, New ListItem("--กรุณาระบุจังหวัด--", "0"))
	End Sub

	Private Sub LoadDistinct1()
		If Not Me.OFFICER_CARD_ADDRESS_PROVINCE.SelectedValue.Equals("0") Then
			OFFICER_CARD_ADDRESS_DISTRICT.DataTextField = "DISTRICT_NAME"
			OFFICER_CARD_ADDRESS_DISTRICT.DataValueField = "DISTRICT_ID"
			OFFICER_CARD_ADDRESS_DISTRICT.DataSource = BL.District.GetAll(Me.OFFICER_CARD_ADDRESS_PROVINCE.SelectedValue)
			OFFICER_CARD_ADDRESS_DISTRICT.DataBind()
		Else
			OFFICER_CARD_ADDRESS_DISTRICT.Items.Clear()
		End If
		OFFICER_CARD_ADDRESS_DISTRICT.Items.Insert(0, New ListItem("--กรุณาระบุเขต/อำเภอ--", "0"))
	End Sub

	Private Sub LoadDistinct2()
		If Not Me.OFFICER_MAIL_ADDRESS_PROVINCE.SelectedValue.Equals("0") Then
			OFFICER_MAIL_ADDRESS_DISTRICT.DataTextField = "DISTRICT_NAME"
			OFFICER_MAIL_ADDRESS_DISTRICT.DataValueField = "DISTRICT_ID"
			OFFICER_MAIL_ADDRESS_DISTRICT.DataSource = BL.District.GetAll(Me.OFFICER_MAIL_ADDRESS_PROVINCE.SelectedValue)
			OFFICER_MAIL_ADDRESS_DISTRICT.DataBind()
		Else
			OFFICER_MAIL_ADDRESS_DISTRICT.Items.Clear()
		End If
		OFFICER_MAIL_ADDRESS_DISTRICT.Items.Insert(0, New ListItem("--กรุณาระบุเขต/อำเภอ--", "0"))
	End Sub

	Private Sub LoadSubDistinct1()
		If Not Me.OFFICER_CARD_ADDRESS_DISTRICT.SelectedValue.Equals("0") Then
			officer_card_address_subICT.DataTextField = "SUB_DISTRICT_NAME"
			officer_card_address_subICT.DataValueField = "SUB_DISTRICT_ID"
			officer_card_address_subICT.DataSource = BL.SubDistrict.AllSubDistrict(Me.OFFICER_CARD_ADDRESS_PROVINCE.SelectedValue, Me.OFFICER_CARD_ADDRESS_DISTRICT.SelectedValue)
			officer_card_address_subICT.DataBind()
		Else
			officer_card_address_subICT.Items.Clear()
		End If
		officer_card_address_subICT.Items.Insert(0, New ListItem("--กรุณาระบุแขวง/ตำบล--", "0"))
	End Sub

	Private Sub LoadSubDistinct2()
		If Not Me.OFFICER_MAIL_ADDRESS_DISTRICT.SelectedValue.Equals("0") Then
			OFFICER_MAIL_ADDRESS_SUB_DISTRICT.DataTextField = "SUB_DISTRICT_NAME"
			OFFICER_MAIL_ADDRESS_SUB_DISTRICT.DataValueField = "SUB_DISTRICT_ID"
			OFFICER_MAIL_ADDRESS_SUB_DISTRICT.DataSource = BL.SubDistrict.AllSubDistrict(Me.OFFICER_MAIL_ADDRESS_PROVINCE.SelectedValue, Me.OFFICER_MAIL_ADDRESS_DISTRICT.SelectedValue)
			OFFICER_MAIL_ADDRESS_SUB_DISTRICT.DataBind()
		Else
			OFFICER_MAIL_ADDRESS_SUB_DISTRICT.Items.Clear()
		End If
		OFFICER_MAIL_ADDRESS_SUB_DISTRICT.Items.Insert(0, New ListItem("--กรุณาระบุแขวง/ตำบล--", "0"))
	End Sub

	Private Sub LoadPostCode1()
		If Not Me.officer_card_address_subICT.SelectedValue.Equals("0") Then
			OFFICER_CARD_ADDRESS_POSTCODE.DataTextField = "POSTCODE"
			OFFICER_CARD_ADDRESS_POSTCODE.DataValueField = "POSTCODE"
			OFFICER_CARD_ADDRESS_POSTCODE.DataSource = BL.PostCode.AllPostCode(Me.OFFICER_CARD_ADDRESS_PROVINCE.SelectedValue, Me.OFFICER_CARD_ADDRESS_DISTRICT.SelectedValue)
			OFFICER_CARD_ADDRESS_POSTCODE.DataBind()
		Else
			OFFICER_CARD_ADDRESS_POSTCODE.Items.Clear()
		End If
		OFFICER_CARD_ADDRESS_POSTCODE.Items.Insert(0, New ListItem("--รหัสไปรษณีย์--", "0"))
	End Sub

	Private Sub LoadPostCode2()
		If Not Me.OFFICER_MAIL_ADDRESS_SUB_DISTRICT.SelectedValue.Equals("0") Then
			OFFICER_MAIL_ADDRESS_POSTCODE.DataTextField = "POSTCODE"
			OFFICER_MAIL_ADDRESS_POSTCODE.DataValueField = "POSTCODE"
			OFFICER_MAIL_ADDRESS_POSTCODE.DataSource = BL.PostCode.AllPostCode(Me.OFFICER_MAIL_ADDRESS_PROVINCE.SelectedValue, Me.OFFICER_MAIL_ADDRESS_DISTRICT.SelectedValue)
			OFFICER_MAIL_ADDRESS_POSTCODE.DataBind()
		Else
			OFFICER_MAIL_ADDRESS_POSTCODE.Items.Clear()
		End If
		OFFICER_MAIL_ADDRESS_POSTCODE.Items.Insert(0, New ListItem("--รหัสไปรษณีย์--", "0"))
	End Sub

	Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnBack.Click
		Response.Redirect("viewprofile.aspx", False)
	End Sub

	Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnNext.Click
		Response.Redirect("viewprofile2.aspx", False)
	End Sub

	Private Sub LoadData()

		Dim registerInfo As Entity.OfficerInfoItem
		registerInfo = GlobalApp.ProfileInfo
		With registerInfo
			Me.OFFICER_CARD_ADDRESS_NO.Text = .officer_card_address_no
			Me.OFFICER_CARD_ADDRESS_MOO.Text = .officer_card_address_moo
			Me.OFFICER_CARD_ADDRESS_BUILDING.Text = .officer_card_address_building
			Me.OFFICER_CARD_ADDRESS_SOI.Text = .officer_card_address_soi
			Me.OFFICER_CARD_ADDRESS_STREET.Text = .officer_card_address_street

			Common.Utility.SelectChoiceByText(OFFICER_CARD_ADDRESS_PROVINCE, .officer_card_address_province)
			Me.LoadDistinct1()
			Common.Utility.SelectChoiceByText(OFFICER_CARD_ADDRESS_DISTRICT, .officer_card_address_district)
			Me.LoadSubDistinct1()
			Common.Utility.SelectChoiceByText(officer_card_address_subICT, .officer_card_address_sub)
			Me.LoadPostCode1()
			Common.Utility.SelectChoiceByText(OFFICER_CARD_ADDRESS_POSTCODE, .officer_card_address_postcode)

			Me.OFFICER_MAIL_ADDRESS_NO.Text = .officer_mail_address_no
			Me.OFFICER_MAIL_ADDRESS_MOO.Text = .officer_mail_address_moo
			Me.OFFICER_MAIL_ADDRESS_BUILDING.Text = .officer_mail_address_building
			Me.OFFICER_MAIL_ADDRESS_SOI.Text = .officer_mail_address_soi
			Me.OFFICER_MAIL_ADDRESS_STREET.Text = .officer_mail_address_street

			Common.Utility.SelectChoiceByText(OFFICER_MAIL_ADDRESS_PROVINCE, .officer_mail_address_province)
			Me.LoadDistinct2()
			Common.Utility.SelectChoiceByText(OFFICER_MAIL_ADDRESS_DISTRICT, .officer_mail_address_district)
			Me.LoadSubDistinct2()
			Common.Utility.SelectChoiceByText(OFFICER_MAIL_ADDRESS_SUB_DISTRICT, .officer_mail_address_sub)
			Me.LoadPostCode2()
			Common.Utility.SelectChoiceByText(OFFICER_MAIL_ADDRESS_POSTCODE, .officer_mail_address_postcode)

			Me.OFFICER_MAIL_PHONE.Text = .officer_mail_phone
			Me.OFFICER_MAIL_EMAIL.Text = .officer_mail_email

		End With
	End Sub

	Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCancel.Click
		GlobalApp.RemoveProfileInfo()
		Me.Response.Redirect("mainpage.aspx", False)
	End Sub

	Protected Sub OFFICER_CARD_ADDRESS_PROVINCE_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles OFFICER_CARD_ADDRESS_PROVINCE.SelectedIndexChanged
		Me.LoadDistinct1()
		Me.LoadSubDistinct1()
		Me.LoadPostCode1()
	End Sub

	Protected Sub OFFICER_CARD_ADDRESS_DISTRICT_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles OFFICER_CARD_ADDRESS_DISTRICT.SelectedIndexChanged
		Me.LoadSubDistinct1()
		Me.LoadPostCode1()
	End Sub

	Protected Sub officer_card_address_subICT_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles officer_card_address_subICT.SelectedIndexChanged
		Me.LoadPostCode1()
	End Sub

	Protected Sub OFFICER_MAIL_ADDRESS_PROVINCE_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles OFFICER_MAIL_ADDRESS_PROVINCE.SelectedIndexChanged
		Me.LoadDistinct2()
		Me.LoadSubDistinct2()
		Me.LoadPostCode2()
	End Sub

	Protected Sub OFFICER_MAIL_ADDRESS_DISTRICT_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles OFFICER_MAIL_ADDRESS_DISTRICT.SelectedIndexChanged
		Me.LoadSubDistinct2()
		Me.LoadPostCode2()
	End Sub

	Protected Sub OFFICER_MAIL_ADDRESS_SUB_DISTRICT_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles OFFICER_MAIL_ADDRESS_SUB_DISTRICT.SelectedIndexChanged
		Me.LoadPostCode2()
	End Sub

End Class